"use strict";
require("test").setup();

var Coroutine = require('coroutine');
var hash = require('hash');

var str_prefix = "CL";

describe("vehicle", function() {
    before(function() {
        httpPost("GET", "/v1.0/api/user/Sync", {})
        // 用户登录
        var t = "123"
        var result = httpPost("POST", "/v1.0/api/user/WebLogin", {
            "phone": "18888888888",
            "password": hash.md5(hash.md5("ZHYQ2020").digest().hex() + t).digest().hex(),
            "t": t,
        })
    });

    after(function() {
        clearDB();
    });

    it("创建车辆", function() {
        var result = httpPost("POST", "/v1.0/api/vehicle/Create", {
            "plate_number": "苏A123456",
            "brand": "奥迪",
            "model": "A8",
            "displacement": "1.5",
            "year": "2019",
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.id, 1);

        //车牌号验证
        result = httpPost("POST", "/v1.0/api/vehicle/Create", {
            "plate_number": "AA123456",
            "brand": "奥迪",
            "model": "A8",
            "displacement": "1.5",
            "year": "2019",
        })
        assert.equal(result.code, "400");
        // assert.equal(result.msg, "");
    });

    it("编辑车辆", function() {
        var result = httpPost("POST", "/v1.0/api/vehicle/Update", {
            "id": 1,
            "plate_number": "苏A654321",
            "year": "2020",
        })
        assert.equal(result.code, "200");
    });

    it("查看车辆列表-车牌模糊查询", function() {
        var result = httpPost("POST", "/v1.0/api/vehicle/List", {
            "plate_number": "A",
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list[0].id, 1);
        assert.equal(result.data.list[0].plate_number, "苏A654321");
        assert.equal(result.data.list[0].brand, "奥迪");
        assert.equal(result.data.list[0].model, "A8");
        assert.equal(result.data.list[0].displacement, "1.5");
        assert.equal(result.data.list[0].year, "2020");
        assert.equal(result.data.total, 1);
    });

    it("车辆列表-统计", function() {
        var result = httpPost("POST", "/v1.0/api/vehicle/Static", {
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.total, 1);
        assert.equal(result.data.disable, 0);
        assert.equal(result.data.occupy, 0);
        assert.equal(result.data.free, 1);
    });

    it("查看车辆详情", function() {
        var result = httpPost("POST", "/v1.0/api/vehicle/Get", {
            "id": 1,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.id, 1);
        assert.equal(result.data.no, str_prefix + "001");
        assert.equal(result.data.plate_number, "苏A654321");
        assert.equal(result.data.brand, "奥迪");
        assert.equal(result.data.model, "A8");
    });

    it("停用-启用 车辆", function() {
        var result = httpPost("POST", "/v1.0/api/vehicle/Update", {
            "id": 1,
            "status": "已停用",
        })
        assert.equal(result.code, "200");

        result = httpPost("POST", "/v1.0/api/vehicle/Update", {
            "id": 1,
            "status": "启用中",
        })
        assert.equal(result.code, "200");
    });

    it("删除车辆", function() {
        var result = httpPost("POST", "/v1.0/api/vehicle/Del", {
            "id": 1,
        })
        assert.equal(result.code, "200");

        result = httpPost("POST", "/v1.0/api/vehicle/List", {
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list.length, 0);
    });

    it("有未完成的预约 不影响停用车辆 -> 停用后，预约产生告警消息", function() {
        var result = httpPost("POST", "/v1.0/api/vehicle/Create", {
            "plate_number": "苏A123456",
            "brand": "奥迪",
            "model": "A8",
            "displacement": "2.0",
            "year": "2008",
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.id, 2);

        result = httpPost("POST", "/v1.0/api/vehicleRecord/Create", {
            "vehicle_id": 2,
            "start_date_time": "2099-12-15 10:00",
            "end_date_time": "2099-12-15 12:00",
            "use_reason": "用车事由",
            "destination": "目的地",
            "address": "目的地地址",
            "reviewed_by": 1,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.id, 1);

        result = httpPost("POST", "/v1.0/api/vehicle/Update", {
            "id": 2,
            "status": "已停用",
        })
        assert.equal(result.code, "200");

        result = httpPost("POST", "/v1.0/api/vehicleRecord/Get", {
            "id": 1,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.id, 1);

        result = httpPost("POST", "/v1.0/api/message/List", {})
        assert.equal(result.code, "200");
        assert.equal(result.data.list[0].id, 1);
        assert.equal(result.data.list[0].content, "您申请的车辆已停用，请及时处理");
    });

    it("有预约 不允许删除车辆", function() {
        var result = httpPost("POST", "/v1.0/api/vehicle/Del", {
            "id": 2,
        })
        assert.equal(result.code, "400");
        assert.equal(result.msg, "车辆已有预约业务,不允许删除车辆");
    });

    it("查看车辆列表-状态查询", function() {
        var result = httpPost("POST", "/v1.0/api/vehicle/List", {
            "status": "已停用",
            "page": 1,
            "limit": 10,
        })
        assert.equal(result.code, "200");
        assert.equal(result.data.list[0].id, 2);
        assert.equal(result.data.list[0].plate_number, "苏A123456");
        assert.equal(result.data.list[0].year, "2008");
        assert.equal(result.data.total, 1);
    });
});

require('test').run();